using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Drawing;

using MULENetworking;

namespace MULEServer
{
    [Serializable]
    public class RemoteServer: MarshalByRefObject, IRemoteServer
    {
        ArrayList clientList;
        ArrayList gameList;
        ArrayList playersList;
        public IGame currentGame;
        ArrayList kickList;
        
        string sessionText;

        public RemoteServer()
        {
            clientList = new ArrayList();
            gameList = new ArrayList();
            playersList = new ArrayList();
            /*playersList = new IPlayer[4];
            for (int i = 0; i < 4; i++)
            {
                playersList[i] = null;
            }*/
            kickList = new ArrayList();
            sessionText = "";
        }

        public string remotingWorks()
        {
            return "yes, remoting works.";
        }

        public string ChatSession
        {
            get { return sessionText; }
        }

        public void SendMessage(string msgToSend)
        {
            if (msgToSend != null)
            {
                lock (sessionText)
                {
                    sessionText += msgToSend;
                }
            }
        }

        public void KickUser(string userName)
        {
            kickList.Add(userName);
        }

        public ArrayList kicks
        {
            get
            {
                return kickList;
            }
            set
            {
                kickList = value;
            }
        }
    

        public ArrayList ChatClients
        {
            get { return clientList; }
        }

        public ArrayList GamesList
        {
            get { return gameList; }
        }

        public ArrayList PlayersList
        {
            get { return playersList; }
        }

        public IGame CurrentGame
        {
            get { return currentGame; }
            set { currentGame = value; }
        }

        public int ConnectToChat(string newUserName)
        {
            int toRet = -1;
            if (newUserName != null)
            {
                lock (clientList)
                {
                    if (clientList.Contains(newUserName) == false)
                    {
                        toRet = clientList.Add(newUserName);
                    }
                }
            }
            else { return 0; }
            return toRet;
        }

        public void DisconnectFromChat(string leavingUserName)
        {
            lock (clientList)
            {
                clientList.Remove(leavingUserName);
            }
        }

        public void DisconnectFromServer(string disconnectUserName)
        {
            //TODO: If I use this, I will pass in a IPlayer object I guess...
        }

        public IGame CreateGame(string creatorsName, string gameName, int level)
        {
            Game newGame;
            IPlayer creatorPlayer = null;
            lock (clientList)
            {
                /*
                foreach (String p in clientList)
                {
                    if (p == creatorsName)
                    {
                        String playa = (String)p;
                        creatorPlayer = new IPlayer(playa, level);
                    }
                }
                 */
            }
            newGame = new Game(gameName, level);
            gameList.Add(newGame);
            currentGame = newGame;
            return newGame;
        }

        public IGame JoinGame(string gameName)
        {
            foreach (IGame g in gameList)
            {
                if (g.GameName == gameName)
                {
                    return g;
                }
            }
            return null;
        }

        public IGame GetGame(string gameName)
        {
            foreach (IGame game in gameList)
            {
                if (game.GameName == gameName)
                {
                    return game;
                }
            }
            return null;
        }

        public void CreatePlayer(String name, IGame game, int avatar, Color color)
        {
            IPlayer newPlayer = new ServerPlayer(name, currentGame.GameLevel, avatar, color);
            if (currentGame.Player1 == null)
            {
                currentGame.Player1 = newPlayer;
                currentGame.ReadyNames.Add(name);
                //playersList[0] = newPlayer;
            }
            else if (currentGame.Player2 == null)
            {
                currentGame.Player2 = newPlayer;
                currentGame.ReadyNames.Add(name);
                //playersList[1] = newPlayer;
            }
            else if (currentGame.Player3 == null)
            {
                currentGame.Player3 = newPlayer;
                currentGame.ReadyNames.Add(name);
                //playersList[2] = newPlayer;
            }
            else if (currentGame.Player4 == null)
            {
                currentGame.Player4 = newPlayer;
                currentGame.ReadyNames.Add(name);
                //playersList[3] = newPlayer;
            }
            playersList.Add(name);
            
        }
    }
}
